package com.zaizai.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zaizai.reggie.common.R;
import com.zaizai.reggie.entity.Employee;
import com.zaizai.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RequestMapping("/employee")
@RestController
public class EmployeeController {
    @Autowired
    public EmployeeService employeeService;


    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee){
        //1.将页面提交的密码password进行md5加密处理
        String password=employee.getPassword();
        password=DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据用户名查数据库
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(employeeLambdaQueryWrapper);
        //3.如果没有查到则返回失败的结果
        if(emp==null){
            return R.error("该用户不存在");
        }
        //4.密码比对，如果不一致则返回登录失败结果
        if(!emp.getPassword().equals(password))
            return R.error("密码错误");
        //查看员工状态，如果为已禁用，则返回员工已禁用结果
        if(emp.getStatus()==0){
            return R.error("账号已禁用");
        }
        long id = Thread.currentThread().getId();
        System.out.println("login当前线程id"+id);

        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);

    }

    @PostMapping("/logout")
    public R<String> loginout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");

    }
    @PostMapping()
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("到这了"+employee);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long empid=(Long)request.getSession().getAttribute("employee");
        employee.setCreateUser(empid);
        employee.setUpdateUser(empid);*/
        long id = Thread.currentThread().getId();
        System.out.println("login当前线程id"+id);

        employeeService.save(employee);
        return R.success("添加成功");
    }


    @GetMapping("/page")
    public R<Page<Employee>> page(int page, int pageSize,String name) {
            //构造分页构造器
            Page pageInfo=new Page(page,pageSize);
            //构造条件构造器
            LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper();
            //添加过滤条件
            queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
            //添加排序条件
            queryWrapper.orderByDesc(Employee::getUpdateTime);
            //执行查询
            employeeService.page(pageInfo,queryWrapper);
            long id = Thread.currentThread().getId();
            System.out.println("login当前线程id"+id);
            log.info("page线程"+id);
            return R.success(pageInfo);

    }
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        if(employee!=null){
            long did = Thread.currentThread().getId();
            log.info("id"+did);

            return R.success(employee);

        }
        return R.error("查找失败");

    }



    @PutMapping()
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        Long empid=(Long)request.getSession().getAttribute("employee");
        employeeService.updateById(employee);
        System.out.println(employee);
        return R.success("修改成功");

    }


}
